package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 攻略评论服务接口实现
*/
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository repsitory;

    @Autowired
    private MongoTemplate template;



    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        repsitory.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        repsitory.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return repsitory.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,3, Sort.Direction.DESC, "_id");
        return DBHelper.query(template,pageable, query,StrategyComment.class, qo);
    }

    @Override
    public void commentThumb(String cid, String uId) {
        //获取点赞用户集合
        StrategyComment comment = this.get(cid);
        List<String> userIds = comment.getThumbuplist();
        //如果用户存在在集合中，此时为取消点赞，点赞数-1
        if(userIds.contains(uId)){
            comment.setThumbupnum(comment.getThumbupnum()-1);
            userIds.remove(uId);
        }else {
            //如果用户不存在在集合中，此时为点赞操作，点赞数+1
            comment.setThumbupnum(comment.getThumbupnum()+1);
            userIds.add(uId);
        }
        this.update(comment);
    }

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        if(StringUtils.hasLength(strategyComment.getId())){
            //update
            this.update(strategyComment);
        }else{
            //save
            this.save(strategyComment);
        }
    }
}
